package com.ant.myagile.test.controller

import junit.framework.TestCase;
import grails.gorm.CriteriaBuilder
import grails.test.mixin.support.*
import groovy.mock.interceptor.MockFor
import org.codehaus.groovy.grails.web.json.JSONObject
import org.junit.Test;
import org.springframework.mock.web.MockMultipartFile
import org.springframework.mock.web.MockMultipartHttpServletRequest

import com.ant.myagile.EnumPosition
import com.ant.myagile.Member
import com.ant.myagile.Project
import com.ant.myagile.Team
import com.ant.myagile.TeamController
import com.ant.myagile.TeamService
import com.ant.myagile.Team_Member
import com.ant.myagile.Team_Project
import com.ant.myagile.Tracking


/**
 * See the API for {@link grails.test.mixin.support.GrailsUnitTestMixin} for usage instructions
 */
@TestFor(TeamController)
@Mock([Member, Team, Team_Member, Project, TeamService, Tracking, Team_Project])
class TeamControllerTest {

	void setUp(){
		println "setup"
		//Resolve conflict with Spring Security
		Member.metaClass.encodePassword = { -> }
		//Add new user and new team
		Date establishedDate = new Date()
		Team newTeam = new Team(teamName: "Ant", mailGroup: "ant@axonactive.vn", establishedDate: establishedDate)
		.save(failOnError: true, flush:true)
		def newUser = new Member(username: "khang.le@axonactive.vn", password: '123213', position: "Junior Java Developer", mobile: "972 731 980")
		.save(failOnError: true, flush:true)
		println "khang setup "+ newUser.id
		println "ant setup "+ newTeam.id

		def newTeamNewUser = new Team_Member()
		newTeamNewUser.member = newUser
		newTeamNewUser.team = newTeam
		newTeamNewUser.position = EnumPosition.DEVELOPER
		newTeamNewUser.save()
	}

	void tearDown() {

		println"teardown"
		def newUser = Member.findByUsername("khang.le@axonactive.vn")
		def newTeam = Team.findByTeamName("Ant")
		Team_Member newTeamNewUser = Team_Member.findByTeamAndMember(newTeam, newUser)

		if (newTeam != null) newTeam.delete(flush: true)
		println "ant "+ newTeam
		if (newUser != null) newUser.delete(flush: true)
		println "khang "+ newUser
		if (newTeamNewUser !=null) newTeamNewUser.delete(flush: true)
		println "newTeamNewUser "+newTeamNewUser

	}
	void testSaveTeam() {
		println "save team"
		assert Team.count() == 1
		controller.springSecurityService = [currentUser:[id:1]]
		params["teamName"]= 'abc'
		params["mailGroup"] = "abc@gmail.com"
		params["description"] = "tea"
		params["establishedDate"] = "2013-07-11"
		def imgContentType = 'image/jpeg'
		def imgContentBytes = '123' as byte[]
		controller.metaClass.request = new MockMultipartHttpServletRequest()
		controller.request.addFile(
				new MockMultipartFile('image', 'myImage.jpg', imgContentType, imgContentBytes)
				)
		controller.saveTeam()
		assert Team.count() == 2
		assertEquals(Team.get(2).teamName,"abc")

	}
	void testUpdateTeam(){
		def team = Team.findByTeamName('Ant')
		params["teamId"]  = team.id
		params["teamName"]= 'abc'
		params["mailGroup"] = "abc@gmail.com"
		params["description"] = "tea"
		params["establishedDate"] = "2013-07-11"
		List<Long> assignedProjIdList = null
		def imgContentType = 'image/jpeg'
		def imgContentBytes = '123' as byte[]
		controller.metaClass.request = new MockMultipartHttpServletRequest()
		controller.request.addFile(
				new MockMultipartFile('image', 'myImage.jpg', imgContentType, imgContentBytes)
				)
		controller.updateTeam()
		assertEquals(team.teamName, "abc")
		assertEquals(team.mailGroup, "abc@gmail.com")
	}
	void testDeleteTeam(){
		println "delete"
		def team = Team.findByTeamName('Ant')
		def member = Member.get(1)
		assert Team.count() == 1
		println "teamName "+ team.teamName
		params["teamId"] = team.id

		controller.deleteTeam()
		println "teamName "+ team.teamName

	}
	void testRemoveMemberFromTeam() {
		println "remove member from team"
		def newUser = Member.findByUsername("khang.le@axonactive.vn")
		def newTeam = Team.findByTeamName("Ant")
		params["teamId"] = newUser.id
		params["memberId"] = newTeam.id
		println "user in remove"+ newUser.id
		println "team in remove"+ newTeam.id
		Team_Member teamMember = Team_Member.findByMemberAndTeam(newUser, newTeam)
		assertEquals(teamMember.member.id, newUser.id)

		controller.removeMemberFromTeam()
		JSONObject data = (JSONObject) response.getJson()
		assertEquals(data.result, 'success')

		teamMember = Team_Member.findByMemberAndTeam(newUser, newTeam)
		assertEquals(teamMember, null)
	}


	void testChangePosition() {
		def newUser = Member.findByUsername("khang.le@axonactive.vn")
		def newTeam = Team.findByTeamName("Ant")

		params["teamId"] = newUser.id
		params["memberId"] = newTeam.id
		params["position"] = EnumPosition.STAKEHOLDER

		Team_Member teamMember = Team_Member.findByMemberAndTeam(newUser, newTeam)
		assertEquals(teamMember.position, EnumPosition.DEVELOPER)

		controller.changePosition()

		JSONObject data = (JSONObject) response.getJson()
		assertEquals(data.result, 'success')

		teamMember = Team_Member.findByMemberAndTeam(newUser, newTeam)
		assertEquals(teamMember.position, EnumPosition.STAKEHOLDER)
	}
	void testcheckExistTeamMember(){
		assertEquals(controller.checkExistTeamMember("khang.le@axonactive.vn",1), true)
	}
	void testSendMailActive(){
		def newTeam = Team.findByTeamName("Ant")

		params['teamId']=Long.parseLong( newTeam.id+"")
		def mailList="khang.le@axonactive.vn"
		params['txaMemberList']=mailList
		controller.sendMailActive(params)
		JSONObject data = (JSONObject) response.getJson()
		assertEquals(data.result,"<div style=\"color: red;\">khang.le@axonactive.vn was added to this team before</div>")
	}
}
